<?php
/**
 *  Base.php Created by : PhpStorm
 *
 * @author: 魏巍 <524314430@qq.com>
 * @date  : 2021/9/24 11:12
 */

namespace app\api\controller;

use app\BaseController;

class Base extends BaseController
{
    /**
     * @param        $data
     * @param string $msg
     * @param int    $status
     */
    protected function success($data, $msg = 'success', $status = 1)
    {
        return json(compact('data', 'status', 'msg'))->send();
    }

    /**
     * @param        $data
     * @param string $msg
     * @param int    $status
     */
    protected function error($data, $msg = 'error', $status = 0)
    {
        return json(compact('data', 'status', 'msg'))->send();
    }

    /**
     * @param       $url
     * @param array $data
     * @param null  $jssesionId
     * @param null  $headers
     *
     * @return array|bool|string
     */
    public function _curl($url, $data = [], $jssesionId = null, $headers = null){
        $headers = $headers ?? [
            'Content-type: application/json;charset=UTF-8;',
            "jssesionId: {$jssesionId}"
        ];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
        $data = json_decode(@curl_exec($curl), true);
        curl_close($curl);
        return $data;
    }
}